package com.huffman.smart_picture.common.auth;


import com.huffman.smart_picture.common.UserSession;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

/**
 * @author DengLin
 * @title session会话
 * @date 2024/06/29
 */
@Component
public class SessionContext {
    private static final ThreadLocal<HttpSession> threadLocal = new ThreadLocal<>();

    public void addSession(HttpSession session) {
        threadLocal.set(session);
    }


    public void removeSession() {
        threadLocal.remove();
    }


    public HttpSession getSession() {
        return threadLocal.get();
    }

    public UserSession getUserSession() {
        return (UserSession) threadLocal.get().getAttribute(AuthConstant.LOGIN_USER);
    }

    public void setCurrentUser(UserSession userSession) {
        threadLocal.get().setAttribute(AuthConstant.LOGIN_USER, userSession);
    }

}
